﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;

using System.IO;

using System.Web.Caching;

namespace Helper
{
    /// <summary>
    /// Web Helper
    /// </summary>
    public static class WebHelper
    {


        /// <summary>
        /// Get int value from QueryString.it is only using in web application
        /// </summary>
        /// <param name="queryString"></param>
        /// <returns></returns>
        public static int GetIntQuery(string queryString)
        {
            return DataConvert.SafeInt(HttpContext.Current.Request.QueryString[queryString]);
        }

        /// <summary>
        /// Get string value from QueryString. it is only using in web application
        /// </summary>
        /// <param name="queryString"></param>
        /// <returns></returns>
        public static string GetStringQuery(string queryString)
        {
            if (string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[queryString]))
                return string.Empty;
            else
                return HttpContext.Current.Request.QueryString[queryString];
        }

        /// <summary>
        /// get cookie value
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <returns>cookie value</returns>
        public static string GetCookieValue(string cookieName)
        {
            HttpContext context = HttpContext.Current;
            if (context.Request.Cookies[cookieName] != null)
            {
                string cookieValue = context.Request.Cookies[cookieName].Value;
                if (!string.IsNullOrEmpty(cookieValue))
                {
                    cookieValue = context.Server.UrlDecode(cookieValue);
                    return cookieValue;
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// set cookie value
        /// </summary>
        /// <param name="cookieName">cookie name</param>
        /// <param name="cookieValue">cookie value</param>
        /// <param name="expireDays">expire days</param>
        public static void SetCookieValue(string cookieName, string cookieValue, double expireDays)
        {
            HttpContext context = HttpContext.Current;
            context.Response.Cookies.Remove(cookieName);
            cookieValue = context.Server.UrlEncode(cookieValue);
            HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
            if (expireDays != 0)
            {
                cookie.Expires = DateTime.Now.AddDays(expireDays);
            }

            context.Response.Cookies.Remove(cookieName);
            context.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// get object from cache
        /// </summary>
        /// <param name="cacheKey">cache key</param>
        /// <returns>object get from cache</returns>
        public static object GetCacheData(string cacheKey)
        {
            if (string.IsNullOrEmpty(cacheKey))
            {
                return null;
            }
            HttpContext context = HttpContext.Current;
            object obj = context.Cache.Get(cacheKey);
            return obj;
        }

        /// <summary>
        /// set object to cache
        /// </summary>
        /// <param name="cacheKey">cache key</param>
        /// <param name="obj">object</param>
        /// <param name="cacheMinutes">cache minutes</param>
        public static void SetCacheData(string cacheKey, object obj, int cacheMinutes)
        {
            if (string.IsNullOrEmpty(cacheKey))
            {
                return;
            }
            HttpContext context = HttpContext.Current;
            context.Cache.Remove(cacheKey);
            context.Cache.Insert(cacheKey, obj, null
                , DateTime.UtcNow.AddMinutes(cacheMinutes)
                , System.Web.Caching.Cache.NoSlidingExpiration);

        }

        /// <summary>
        /// remove object from cache
        /// </summary>
        /// <param name="cacheKey">cache key</param>
        public static void RemoveCacheData(string cacheKey)
        {
            HttpContext context = HttpContext.Current;
            context.Cache.Remove(cacheKey);
        }

        /// <summary>
        /// Get data from session
        /// </summary>
        /// <typeparam name="T">type</typeparam>
        /// <param name="key">session key</param>
        /// <returns>an object that store in session.</returns>
        public static T GetSession<T>(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return default(T);
            }
            if (HttpContext.Current.Session[key] != null)
            {
                return (T)HttpContext.Current.Session[key];
            }
            return default(T);
        }

        /// <summary>
        /// save data to session
        /// </summary>
        /// <param name="key">session key</param>
        /// <param name="obj">object to save to session</param>
        public static void SetSession(string key, object obj)
        {
            HttpContext.Current.Session[key] = obj;
        }

        /// <summary>
        /// get data from application
        /// </summary>
        /// <typeparam name="T">type</typeparam>
        /// <param name="key">application key</param>
        /// <returns>an object that store in application</returns>
        public static T GetApplication<T>(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return default(T);
            }
            if (HttpContext.Current.Application[key] != null)
            {
                return (T)HttpContext.Current.Application[key];
            }
            return default(T);
        }

        /// <summary>
        /// save data to application
        /// </summary>
        /// <param name="key">application key</param>
        /// <param name="obj">object</param>
        public static void SetApplication(string key, object obj)
        {
            HttpContext.Current.Application.Lock();
            HttpContext.Current.Application[key] = obj;
            HttpContext.Current.Application.UnLock();
        }

        /// <summary>
        /// get view state
        /// </summary>
        /// <typeparam name="T">type</typeparam>
        /// <param name="viewState">view state</param>
        /// <param name="key">key</param>
        /// <returns>an object that store in view state</returns>
        public static T GetViewState<T>(StateBag viewState, string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return default(T);
            }

            if (viewState[key] != null)
            {
                return (T)viewState[key];
            }
            return default(T);
        }

        /// <summary>
        /// set view state
        /// </summary>
        /// <param name="viewState">view state</param>
        /// <param name="key">key</param>
        /// <param name="obj">object</param>
        public static void SetViewState(StateBag viewState, string key, object obj)
        {
            viewState[key] = obj;
        }



    }
}
